package com.songcha.library_business.proxy.database

import com.songcha.library_business.proxy.userinfo.UserInfoManager
import com.songcha.library_common.util.LogUtil
import com.songcha.library_common.util.TimeUtil
import com.songcha.library_database_douyue.bean.TodayObtainGoldCountDBBean
import com.songcha.library_database_douyue.greendao.TodayObtainGoldCountDBBeanDao
import com.songcha.library_database_douyue.manager.GreenDaoManager


class TodayObtainGoldCountDaoManager {
    companion object{
        @JvmStatic
        fun addTodayObtainGoldCount(gold:Int):Int{
            if(gold<=0) return 0
            try{
                val dao=GreenDaoManager.getInstance().todayObtainGoldCountDao
                val bean=
                    TodayObtainGoldCountDBBean()
                bean.userId=UserInfoManager.userId
                val qb=dao.queryBuilder().where(TodayObtainGoldCountDBBeanDao.Properties.UserId.eq(UserInfoManager.userId))
                val list=qb.list()
                if(list.size==0){
                    bean.gold=gold
                    bean.startTime=TimeUtil.getCurrentTimeSecond()
                    dao.insert(bean)
                    return bean.gold or (1 shl 31)
                }else {
                    val item=list[0]
                    val todayZero=TimeUtil.getTodayZeroTimeStamp()
                    bean.id=item.id
                    if(item.startTime>=todayZero && item.startTime<todayZero+86400){
                        bean.gold=item.gold+gold
                        bean.startTime=item.startTime
                        dao.update(bean)
                        return bean.gold
                    }else{
                        bean.gold=gold
                        bean.startTime=TimeUtil.getCurrentTimeSecond()
                        dao.update(bean)
                        return bean.gold or (1 shl 31)
                    }

                }
            }catch (e:Exception){
                LogUtil.log("保存今天获取金币异常")
                e.printStackTrace()
                return 0
            }
        }

        @JvmStatic
        fun getTodayObtainGoldCount():Int{
            try{
                val dao=GreenDaoManager.getInstance().todayObtainGoldCountDao
                val qb=dao.queryBuilder().where(TodayObtainGoldCountDBBeanDao.Properties.UserId.eq(UserInfoManager.userId))
                val list=qb.list()
                if(list.size>0){
                    val item=list[0]
                    val todayZero=TimeUtil.getTodayZeroTimeStamp()
                    if(item.startTime>=todayZero && item.startTime<todayZero+86400){
                        return item.gold
                    }else{
                        val bean=
                            TodayObtainGoldCountDBBean()
                        bean.id=item.id
                        bean.userId=UserInfoManager.userId
                        bean.gold=0
                        bean.startTime=TimeUtil.getCurrentTimeSecond()
                        dao.update(bean)
                        return 0
                    }
                }else{
                    return 0
                }
            }catch (e:Exception){
                e.printStackTrace()
                return 0
            }

        }

    }
}